﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilityLibrary.Ormapping;
using WZ.Entity;

namespace WZ.Stock.Data
{
    public class AnnouncementDAL : AttributeAdapterBase<Announcement>
    {
        static WZ.DC.Config.DBConfig<Announcement> dbConfig = new WZ.DC.Config.DBConfig<Announcement>();
        public AnnouncementDAL()
            : base(dbConfig.DBConnStr)
        {
        }
        public List<Announcement> GetAllAnnouncement()
        {
            return SelectTo().ToList();
        }
        public int InsertData(Announcement model)
        {
            List<dic_AnnouncementKeyword> keywords = new dic_AnnouncementKeywordDAL().SelectTo().ToList();
            List<dic_AnnouncementKeyword> c = keywords.FindAll(a => model.announcementTitle.Contains(a.Keyword));
            int levelId = 0;
            int noticetype = 0;
            foreach (var item in c)
            {
                if (item.LevelId > levelId)
                {
                    levelId = item.LevelId;
                }
                if (item.TypeId == 1)
                {
                    noticetype = item.TypeId;
                }
                string sql = @"INSERT INTO [Announcement_Keyword]([KeywordId],[AnnoucementId],TypeId)VALUES(@KeywordId,@AnnoucementId,@TypeId)";
                DataBase.MakeParam("KeywordId", item.ID);
                DataBase.MakeParam("AnnoucementId", model.announcementId);
                DataBase.MakeParam("TypeId", item.TypeId);
                RunSql(sql);
            }
            model.NoticeType = noticetype;
            model.Level = levelId;
            return model.Id = Insert(model);
        }

        /// <summary>
        /// 公告是否存在
        /// </summary>
        /// <param name="AnnoucementId"></param>
        /// <returns></returns>
        public bool ExistAnnouncemnet(string AnnoucementId)
        {
            string sql = @"
IF EXISTS(SELECT ID FROM dbo.Announcement WHERE announcementId=@annoucementID)
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 0
END";
            DataBase.MakeParam("annoucementID", AnnoucementId);
            return ((int)DataBase.RunSqlObject(sql)) == 1;
        }
        public List<Announcement> SearchAnnoumentList(string[] keywordId, int typeid, string companyCode, DateTime? beginTime, DateTime? endTime, string searchKeyword)
        {
            string sql = @"
SELECT 
    secCode
    ,secName
    ,announcementId
    ,announcementTitle
    ,announcementdateTime
    ,adjunctUrl
    ,ROW_NUMBER() over (order by announcementId desc) as rowNum
FROM dbo.Announcement";
            List<string> where = new List<string>();
            if (keywordId.Length > 0)
            {
                where.Add(string.Format("announcementId in (SELECT AnnoucementId FROM dbo.Announcement_Keyword WHERE KeywordId in ({0}))", string.Join(",", keywordId)));
            }
            if (!string.IsNullOrEmpty(companyCode))
            {
                where.Add(string.Format("secCode='{0}'", companyCode));
            }
            if (beginTime.HasValue)
            {
                where.Add(string.Format("announcementdateTime>'{0}'", beginTime.Value.ToString("yyyy-MM-dd")));
            }
            if (endTime.HasValue)
            {
                where.Add(string.Format("announcementdateTime<'{0}'", endTime.Value.ToString("yyyy-MM-dd")));
            }
            if (!string.IsNullOrEmpty(searchKeyword))
            {
                where.Add(string.Format("announcementTitle like '%{0}%'", searchKeyword));
            }
            if (typeid > -1)
            {
                //where.Add("announcementId in (SELECT AnnoucementId FROM dbo.Announcement_Keyword WHERE typeId=1)");
                where.Add("NoticeType=" + typeid);
            }
            if (where.Count > 0)
            {
                sql = string.Format("{0} WHERE {1}", sql, string.Join(" AND ", where));
            }

            return SelectTo(sql).ToList();
        }
    }
}
